﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Windows.Controls;
using Dsl.Ped.Framework.Common;
using Dsl.Ped.Framework.Messages;
using Dsl.Ped.Framework.Projections;

namespace Dsl.Ped.AddIn.Beeps.Generated
{
    /// <summary>
    /// This is the class that enhances the <see cref="BeepsBeep"/> class 
    /// provided in the model of projections, these are organized into containers
    /// </summary>
    public partial class BeepsBeep : IProjective, IDataErrorInfo
    {
        /// <summary>
        /// Gets the collection of <see cref="IProjectionContainer"/> objects
        /// </summary>
        /// <returns>A collection of <see cref="IProjectionContainer"/> objects belonging 
        /// to the <see cref="BeepsBeep"/> class</returns>
        public System.Collections.Generic.List<IProjectionContainer> GetProjectionContainers()
        {
            return new List<IProjectionContainer>()
                {
                    new ProjectionContainer("Number")
                        .RegisterProjection("Number", ValueProjectionFactory.GetLabelProjection(this, () => frequence))
                        .RegisterProjection("Combo", ValueProjectionFactory.GetComboBoxProjection(this, () => frequence, new decimal[] { 10, 15, 20, 25, 30, 35, 40, 45, 50, 55, 60, 65, 70 }))
                        .RegisterTransition(Label.MouseMoveEvent, "Number", "Combo")
                        .RegisterTransition(Control.MouseLeaveEvent, "Combo", "Number")
                };
        }

        #region IDataErrorInfo

        public string Error
        {
            get
            {
                return string.Empty;
            }
        }

        /// <summary>
        /// Validates that the property frequence is not higher then 50,
        /// if so, just notify the error via <see cref="FrameworkMessenger"/>.
        /// Use the <see cref="NotifyMessage"/> for the notification.
        /// </summary>
        /// <param name="propertyName">Name of the property being alidated</param>
        /// <returns>String containing an error, null if the property is valid</returns>
        public string this[string propertyName]
        {
            get
            {
                string validationResult;
                switch (propertyName)
                {
                    case "frequence":
                        validationResult = ValidateFrequency();
                        
                        var messenger = FrameworkMessenger.GetMessenger(this);
                        if (messenger != null) messenger.Send(new NotifyMessage(this, propertyName, validationResult, NotifyType.Error));
                        break;
                    default:
                        throw new ApplicationException("Unknown Property being validated.");
                }

                return validationResult;
            }
        }

        /// <summary>
        /// Validate the Frequency 
        /// </summary>
        /// <returns>Error indicating the problem with validation, null if the frequency is valid</returns>
        private string ValidateFrequency()
        {
            if (this.frequence > 50)
            {
                return "Maximal allowed frequency is 50, but is " + this.frequence;
            } 
            else
            {
                return null;
            }
        }

        #endregion
    }
}
